package com.ai.tool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TxtUtil {
	public static String readTxt(String filePath) {
		File file = new File(filePath);
		if (!file.isFile()) {
			throw new RuntimeException(String.format("%s不是一个文件", filePath));
		}
		StringBuffer sb = new StringBuffer();
		List<String> lines = readTxtByRow(file);
		for (String line : lines) {
			sb.append(line).append('\n');
		}
		return sb.toString();
	}

	/**
	 * 读取txt文件 返回单个字符串
	 * 
	 * @param file
	 */
	public static String readTxt(File file) {
		StringBuffer sb = new StringBuffer();
		List<String> lines = readTxtByRow(file);
		for (String line : lines) {
			sb.append(line);
		}
		return sb.toString();
	}

	public static List<String> readTxtByRow(File file) {
		return readTxtByRow(file, "UTF8");
	}

	/**
	 * 按行读取txt文件
	 * 
	 * @param file
	 */
	public static List<String> readTxtByRow(File file, String charsetName) {
		List<String> lineList = new ArrayList<String>();
		// try {
		// (
		// }
		// FileInputStream fis = new FileInputStream(file);
		// InputStreamReader isr = new InputStreamReader(fis, charsetName);
		// BufferedReader br = new BufferedReader(isr)) {
		//
		// String line = null;
		// while (!"".equals(line = br.readLine())) {
		// lineList.add(line);
		// }
		// } catch (IOException e) {
		// e.printStackTrace();
		// throw new RuntimeException("读文件出错：" + file.getName(), e);
		// }
		return lineList;
	}

	/**
	 * 按行和列读取txt文件
	 * 
	 * @param file
	 */
	public static List<String[]> readTxtByRowCol(File file) {
		List<String> lineList = readTxtByRow(file);

		List<String[]> lineArrList = new ArrayList<String[]>();
		for (String line : lineList) {
			String[] lineArr = line.split(" ");
			lineArrList.add(lineArr);
		}
		return lineArrList;
	}

	public static String readTxtLine(File file, int index) {
		return null;
	}

	/**
	 * 写txt文件
	 * 
	 * @param file
	 * @param content
	 *            整个文本内容
	 * @param append
	 *            是否为追加内容
	 */
	public static void writeTxt(File file, String content, boolean append) {
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				throw new RuntimeException("创建文件出错：" + file.getName(), e);
			}
		}

		FileOutputStream fos = null;
		Writer out = null;
		// FileWriter fw = null;
		try {
			fos = new FileOutputStream(file, append);
			out = new OutputStreamWriter(fos, "UTF-8");
			out.write(content);
			out.flush();
			// fw = new FileWriter(file,append);
			// fw.write(content);
		} catch (IOException e) {
			throw new RuntimeException("写文件出错：" + file.getName(), e);
		} finally {
			try {
				// fw.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				// e.printStackTrace();
			}
			try {
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				// e.printStackTrace();
			}
		}
	}

	/**
	 * 写txt文件
	 * 
	 * @param file
	 * @param lineList
	 *            行内容list
	 */
	public static void writeTxt(String filePath, Collection<String> lineList) {
		File file = new File(filePath);
		writeTxt(file, lineList);
	}

	/**
	 * 写txt文件
	 * 
	 * @param file
	 * @param lineList
	 *            行内容list
	 */
	public static void writeTxt(File file, Collection<String> lineList) {
		StringBuffer sb = new StringBuffer();
		for (String line : lineList) {
			sb.append(line).append("\r\n");
		}
		writeTxt(file, sb.toString(), false);
	}

	/**
	 * 更新txt文件
	 * 
	 * @param file
	 * @param index
	 *            行序号
	 * @param str
	 *            行内容
	 */
	public static void updateTxtLine(File file, int index, String str) {
		if (file.exists() == false) {
			throw new RuntimeException("文件不存在:" + file.getName());
		}

		List<String> lineList = readTxtByRow(file);
		if (lineList.size() < index) {
			throw new RuntimeException("文件没有" + index + "行:" + file.getName());
		}
		lineList.set(index - 1, str);
		writeTxt(file, lineList);
	}

}
